package cn.ibizlab.odoo.core.odoo_mrp.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imrp_document;
import cn.ibizlab.odoo.core.odoo_mrp.domain.Mrp_document;
import cn.ibizlab.odoo.core.odoo_mrp.filter.Mrp_documentSearchContext;
import cn.ibizlab.odoo.core.odoo_mrp.service.IMrp_documentService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[生产文档] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mrp_documentServiceImpl implements IMrp_documentService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imrp_documentClientService mrp_documentClientService;

    @Override
    public boolean update(Mrp_document mrp_document) {
        boolean bOk = false ;
        Imrp_document clientModel = this.convert2Model(mrp_document,null);
        mrp_documentClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mrp_document) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(Mrp_document mrp_document) {
        boolean bOk = false ;
        Imrp_document clientModel = this.convert2Model(mrp_document,null);
        mrp_documentClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mrp_document);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mrp_documentClientService.removeBatch(mrp_documents) ;

    }

    @Override
    public void createBatch(List<Mrp_document> mrp_documents, int batchSize) {

                        //mrp_documentClientService.createBatch(mrp_documents,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imrp_document clientModel = mrp_documentClientService.createModel();
        clientModel.setId(id);
        mrp_documentClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public Mrp_document get(Integer id) {
        Imrp_document clientModel = mrp_documentClientService.createModel();
        clientModel.setId(id);
        mrp_documentClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mrp_document get(Mrp_document mrp_document) {
        Mrp_document retmrp_document = this.get(mrp_document.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmrp_document.getClass(), mrp_document.getClass(), false);
        copier.copy(retmrp_document, mrp_document, null);
        return mrp_document ;
    }

    @Override
    public void updateBatch(List<Mrp_document> mrp_documents, int batchSize) {

        //mrp_documentClientService.updateBatch(mrp_documents,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mrp_document> searchDefault(Mrp_documentSearchContext context) {
        List<Mrp_document> list = new ArrayList<Mrp_document>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_document> clientModelList = mrp_documentClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_document clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_document>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_document> selectByOdoo_create(Integer id){
        Mrp_documentSearchContext context = new Mrp_documentSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_document> list = new ArrayList<Mrp_document>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_document> clientModelList = mrp_documentClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_document clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_document>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_document> selectByOdoo_write(Integer id){
        Mrp_documentSearchContext context = new Mrp_documentSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_document> list = new ArrayList<Mrp_document>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_document> clientModelList = mrp_documentClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_document clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_document>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imrp_document convert2Model(Mrp_document domain , Imrp_document model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mrp_documentClientService.createModel() ;
        }

         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getRes_fieldDirtyFlag())
            model.setRes_field(domain.getRes_field());
         if(domain.getRes_nameDirtyFlag())
            model.setRes_name(domain.getRes_name());
         if(domain.getRes_model_nameDirtyFlag())
            model.setRes_model_name(domain.getRes_model_name());
         if(domain.getDatas_fnameDirtyFlag())
            model.setDatas_fname(domain.getDatas_fname());
         if(domain.getTheme_template_idDirtyFlag())
            model.setTheme_template_id(domain.getTheme_template_id());
         if(domain.getMimetypeDirtyFlag())
            model.setMimetype(domain.getMimetype());
         if(domain.getRes_idDirtyFlag())
            model.setRes_id(domain.getRes_id());
         if(domain.getStore_fnameDirtyFlag())
            model.setStore_fname(domain.getStore_fname());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getLocal_urlDirtyFlag())
            model.setLocal_url(domain.getLocal_url());
         if(domain.getKeyDirtyFlag())
            model.setKey(domain.getKey());
         if(domain.getIbizpublicDirtyFlag())
            model.setIbizpublic(domain.getIbizpublic());
         if(domain.getRes_modelDirtyFlag())
            model.setRes_model(domain.getRes_model());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getThumbnailDirtyFlag())
            model.setThumbnail(domain.getThumbnail());
         if(domain.getUrlDirtyFlag())
            model.setUrl(domain.getUrl());
         if(domain.getFile_sizeDirtyFlag())
            model.setFile_size(domain.getFile_size());
         if(domain.getAccess_tokenDirtyFlag())
            model.setAccess_token(domain.getAccess_token());
         if(domain.getCompany_idDirtyFlag())
            model.setCompany_id(domain.getCompany_id());
         if(domain.getIr_attachment_idDirtyFlag())
            model.setIr_attachment_id(domain.getIr_attachment_id());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getTypeDirtyFlag())
            model.setType(domain.getType());
         if(domain.getChecksumDirtyFlag())
            model.setChecksum(domain.getChecksum());
         if(domain.getDb_datasDirtyFlag())
            model.setDb_datas(domain.getDb_datas());
         if(domain.getIndex_contentDirtyFlag())
            model.setIndex_content(domain.getIndex_content());
         if(domain.getDescriptionDirtyFlag())
            model.setDescription(domain.getDescription());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getWebsite_urlDirtyFlag())
            model.setWebsite_url(domain.getWebsite_url());
         if(domain.getWebsite_idDirtyFlag())
            model.setWebsite_id(domain.getWebsite_id());
         if(domain.getDatasDirtyFlag())
            model.setDatas(domain.getDatas());
         if(domain.getPriorityDirtyFlag())
            model.setPriority(domain.getPriority());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Mrp_document convert2Domain( Imrp_document model ,Mrp_document domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mrp_document();
        }

        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getRes_fieldDirtyFlag())
            domain.setRes_field(model.getRes_field());
        if(model.getRes_nameDirtyFlag())
            domain.setRes_name(model.getRes_name());
        if(model.getRes_model_nameDirtyFlag())
            domain.setRes_model_name(model.getRes_model_name());
        if(model.getDatas_fnameDirtyFlag())
            domain.setDatas_fname(model.getDatas_fname());
        if(model.getTheme_template_idDirtyFlag())
            domain.setTheme_template_id(model.getTheme_template_id());
        if(model.getMimetypeDirtyFlag())
            domain.setMimetype(model.getMimetype());
        if(model.getRes_idDirtyFlag())
            domain.setRes_id(model.getRes_id());
        if(model.getStore_fnameDirtyFlag())
            domain.setStore_fname(model.getStore_fname());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getLocal_urlDirtyFlag())
            domain.setLocal_url(model.getLocal_url());
        if(model.getKeyDirtyFlag())
            domain.setKey(model.getKey());
        if(model.getIbizpublicDirtyFlag())
            domain.setIbizpublic(model.getIbizpublic());
        if(model.getRes_modelDirtyFlag())
            domain.setRes_model(model.getRes_model());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getThumbnailDirtyFlag())
            domain.setThumbnail(model.getThumbnail());
        if(model.getUrlDirtyFlag())
            domain.setUrl(model.getUrl());
        if(model.getFile_sizeDirtyFlag())
            domain.setFile_size(model.getFile_size());
        if(model.getAccess_tokenDirtyFlag())
            domain.setAccess_token(model.getAccess_token());
        if(model.getCompany_idDirtyFlag())
            domain.setCompany_id(model.getCompany_id());
        if(model.getIr_attachment_idDirtyFlag())
            domain.setIr_attachment_id(model.getIr_attachment_id());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getTypeDirtyFlag())
            domain.setType(model.getType());
        if(model.getChecksumDirtyFlag())
            domain.setChecksum(model.getChecksum());
        if(model.getDb_datasDirtyFlag())
            domain.setDb_datas(model.getDb_datas());
        if(model.getIndex_contentDirtyFlag())
            domain.setIndex_content(model.getIndex_content());
        if(model.getDescriptionDirtyFlag())
            domain.setDescription(model.getDescription());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getWebsite_urlDirtyFlag())
            domain.setWebsite_url(model.getWebsite_url());
        if(model.getWebsite_idDirtyFlag())
            domain.setWebsite_id(model.getWebsite_id());
        if(model.getDatasDirtyFlag())
            domain.setDatas(model.getDatas());
        if(model.getPriorityDirtyFlag())
            domain.setPriority(model.getPriority());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
